﻿using System;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Soundteller.Application.Facade.Interfaces;

namespace Soundteller.Application.Components
{
    public class AudioComponent : GameComponent
    {
        private readonly IAudioFacade _audioFacade;
        private readonly IAudioVisualFacade _audioVisualFacade;

        public AudioComponent(Game game, IAudioFacade audioFacade, IAudioVisualFacade audioVisualFacade)
            : base(game)
        {
            if (audioFacade == null) throw new ArgumentNullException("audioFacade");
            if (audioVisualFacade == null) throw new ArgumentNullException("audioVisualFacade");

            _audioFacade = audioFacade;
            _audioVisualFacade = audioVisualFacade;
        }

        public override void Initialize()
        {
            Debug.WriteLine("{0}.Initialize()", GetType().Namespace, null);

            _audioFacade.LoadSong(@"Content\Test\DummySong.json");

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            _audioFacade.TickMetronome(gameTime.ElapsedGameTime);

            var sound = _audioFacade.PopSoundBuffer();
            while (sound != null)
            {
                sound.SoundEffect.Play(1f, -1f, 0f);
                sound = _audioFacade.PopSoundBuffer();
            }
        }
    }
}